package com.zhouyu.mybatis.spring;

import com.zhouyu.mapper.UserMapper;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.stereotype.Component;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author mimao funfind@yeah.net
 * @date ${{
 */
//@Component
public class WangFactoryBean implements FactoryBean {

	private Class mapperInterface;

	private SqlSession sqlsession;


	public WangFactoryBean(Class mapperInterface) {
		this.mapperInterface = mapperInterface;
	}


	public void setSqlSession(SqlSessionFactory sqlSessionFactory){
		sqlSessionFactory.getConfiguration().addMapper(mapperInterface);
		this.sqlsession = sqlSessionFactory.openSession();

	}

	@Override
	public Object getObject() throws Exception {

//		Proxy.newProxyInstance(WangFactoryBean.class.getClassLoader(), new Class[]{UserMapper.class}, new InvocationHandler() {
//			@Override
//			public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//				System.out.println(method.getName());
//				return null;
//			}
//		}
//		);
//		return UserMapper.class;

		return sqlsession.getMapper(mapperInterface);
	}

	@Override
	public Class<?> getObjectType() {
		return UserMapper.class;
	}
}
